<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 500px;
            height: 400px;
            background-color: pink;
            margin: 50px auto;
            overflow: hidden;
        }

        .box h2 {
            height: 50px;
            width: 50%;
            line-height: 50px;
            text-align: center;
            float: left;
            background-color: rgb(249, 158, 173);
            cursor: pointer;
        }
        .box h2.active{
            color: white;
        }
        .box .content{
            width: 100%;
            height: 350px;
            margin-top: 50px;
        }
        .box .content ul{
            width: 100%;
            height: 100%;
            font-size: 24px;
        }
        .box .content ul li{
            box-sizing: border-box;
            padding-left: 20px;
            margin-bottom: 20px;
            
        }
    </style>
</head>

<body>
    <div class="box">
        <h2>视频</h2>
        <h2>直播</h2>
        <div class="content">
            <!-- <ul>
                <li>女童幼儿园内摔倒身亡 家长发声
                </li>
                <li>鸿海董事长谈歌尔丢单
                </li>
                <li>女童幼儿园内摔倒身亡 家长发声
                </li>
                <li>多位市委书记、市长密集赴上海
                </li>
                <li>鸿海董事长谈歌尔丢单
                </li>
            </ul> -->
        </div>
    </div>
    
</body>

</html>
<script>
    //定义路由表

    let routes = [
        { path: '/video', component: videoPage },
        { path: '/live', component: livePage }
    ]

    //给多个标签绑定事件
    let h2s = [...document.querySelectorAll('h2')]
    // console.log(h2s);
    h2s.forEach((item, index) => {
        // console.log(item);
        item.onclick = function (e) {
            e.preventDefault()
            //调用history.pushState()添加一个新的历史记录(同时实现路由地址的切换)
            history.pushState({}, '', routes[index].path)
            routes[index].component()
            // if( e.target.className === 'video' || e.target.className === 'live' ){
            //     item.style.color = 'white'
            // }else{
            //     item.style.color = 'black'
            // }  
            h2s.forEach((item)=>{
                console.log(item);
                item.classList.remove('active')
            })
            item.classList.add('active')
        }
        


    })
    function videoPage() {
        document.querySelector('.content').innerHTML = 
        `<ul>
            <li>女童幼儿园内摔倒身亡 家长发声
            </li>
            <li>鸿海董事长谈歌尔丢单
            </li>
            <li>女童幼儿园内摔倒身亡 家长发声
            </li>
            <li>多位市委书记、市长密集赴上海
            </li>
            <li>鸿海董事长谈歌尔丢单
            </li>
        </ul>`
    }
    function livePage() {
        document.querySelector('.content').innerHTML = 
        `<ul>
            <li>蔡徐坤唱跳rep篮球
            </li>
            <li>小黑子露出鸡脚了
            </li>
            <li>你打的算盘我在山西都听到了
            </li>
            <li>谢谢你蒙古人，不然我一直被蒙在鼓里
            </li>
            <li>鸿海董事长谈歌尔丢单
            </li>
        </ul>`
    }

</script>